package com.yxq.mdt.app.modules.auction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.auction.adapter.AuctionMyTeamAdapter;
import com.yxq.mdt.app.modules.auction.bean.HonorValueBean;
import com.yxq.mdt.app.modules.auction.bean.MyTeamFansBean;
import com.yxq.mdt.app.modules.auction.model.AuctionModel;
import com.yxq.mdt.app.modules.auction.presenter.AuctionMyTeamContract;
import com.yxq.mdt.app.modules.auction.presenter.AuctionMyTeamPresenter;
import com.yxq.mdt.app.modules.bottomsheet.ShareSheetDialog;
import com.yxq.mdt.app.net.ApiModel;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的团队
 */
public class AuctionMyTeamAc extends BaseActivity<AuctionMyTeamPresenter> implements AuctionMyTeamContract.View {
    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.auction_withdrawal)
    TextView auctionWithdrawal;
    @BindView(R.id.auction_my_team_num)
    TextView auctionMyTeamNum;
    @BindView(R.id.auction_my_team_recyclerView)
    RecyclerView auctionMyTeamRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.auction_is_show_team)
    LinearLayout auctionIsShowTeam;
    AuctionMyTeamAdapter auctionMyTeamAdapter;
    AuctionMyTeamAdapter auctionMyTeamAdapterSec;
    @BindView(R.id.auction_identity)
    TextView auctionIdentity;
    @BindView(R.id.auction_contribution)
    TextView auctionContribution;
    @BindView(R.id.auction_is_lint)
    LinearLayout auctionIsLint;
    @BindView(R.id.auction_regimental_commander)
    TextView auctionRegimentalCommander;
    int isShow = 0;
    @BindView(R.id.my_team_rule)
    ImageView myTeamRule;
    @BindView(R.id.my_contribution_rule)
    ImageView myContributionRule;
    @BindView(R.id.auction_auction)
    TextView auctionAuction;
    @BindView(R.id.auction_is_show_profit)
    LinearLayout auctionIsShowProfit;
    @BindView(R.id.layout_team)
    LinearLayout layoutTeam;
    ShareSheetDialog shareSheetDialog;
    @BindView(R.id.auction_layout_fans)
    LinearLayout auctionLayoutFans;
    @BindView(R.id.auction_my_team_num_sec)
    TextView auctionMyTeamNumSec;
    @BindView(R.id.auction_layout_fans_sec)
    LinearLayout auctionLayoutFansSec;
    @BindView(R.id.auction_my_team_recyclerView_sec)
    RecyclerView auctionMyTeamRecyclerViewSec;
    @BindView(R.id.auction_contribution_last_mouth)
    TextView auctionContributionLastMouth;
    @BindView(R.id.auction_my_team_num_txt)
    TextView auctionMyTeamNumTxt;
    @BindView(R.id.auction_my_team_num_sec_txt)
    TextView auctionMyTeamNumSecTxt;
    int pageSec = 1;
    int type = 1;
    @BindView(R.id.iv_line_tab_fans)
    ImageView ivLineTabFans;
    @BindView(R.id.iv_line_tab_fans_sec)
    ImageView ivLineTabFansSec;

    @Override
    public int getLayoutId() {
        return R.layout.activity_auction_my_team;
    }

    @Override
    public void initView() {
        mPresenter = new AuctionMyTeamPresenter(this, new AuctionModel());

        auctionMyTeamRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        auctionMyTeamAdapter = new AuctionMyTeamAdapter(R.layout.item_my_team_list, null);
        auctionMyTeamRecyclerView.setAdapter(auctionMyTeamAdapter);
        auctionMyTeamAdapter.setEmptyView(R.layout.layout_empty_team_fans_view, auctionMyTeamRecyclerView);
        auctionMyTeamAdapter.isUseEmpty(false);

        auctionMyTeamRecyclerViewSec.setLayoutManager(new LinearLayoutManager(mActivity));
        auctionMyTeamAdapterSec = new AuctionMyTeamAdapter(R.layout.item_my_team_list, null);
        auctionMyTeamRecyclerViewSec.setAdapter(auctionMyTeamAdapterSec);
        auctionMyTeamAdapterSec.setEmptyView(R.layout.layout_empty_team_fans_view, auctionMyTeamRecyclerViewSec);
        auctionMyTeamAdapterSec.isUseEmpty(false);

        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getHonorValue();
            if (type == 1) {
                page = 1;
                mPresenter.getAuctionFansList(page, 30);
            } else {
                pageSec = 1;
                AuctionModel.getPmUserFansWithLeveltwo(pageSec, 30, new ApiModel.CommonCallback<MyTeamFansBean>() {
                    @Override
                    public void onSuccess(MyTeamFansBean myTeamFansBean) {
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                        auctionMyTeamNumSec.setText("(" + myTeamFansBean.getTotal() + ")");
                        auctionMyTeamAdapterSec.setNewData(myTeamFansBean.getList());
                        auctionMyTeamAdapterSec.isUseEmpty(!(auctionMyTeamAdapterSec.getData().size() > 0));
                    }

                    @Override
                    public void onError(String errMsg) {
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                    }
                });
            }
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            if (type == 1) {
                page++;
                mPresenter.getAuctionFansList(page, 30);
            } else {
                pageSec++;
                AuctionModel.getPmUserFansWithLeveltwo(pageSec, 30, new ApiModel.CommonCallback<MyTeamFansBean>() {
                    @Override
                    public void onSuccess(MyTeamFansBean myTeamFansBean) {
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                        if (myTeamFansBean.getList() != null && myTeamFansBean.getList().size() != 0) {
                            auctionMyTeamAdapterSec.addData(myTeamFansBean.getList());
                        } else {
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    }

                    @Override
                    public void onError(String errMsg) {
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                    }
                });
            }
        });
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        refreshLayout.autoRefresh();
        AuctionModel.getPmUserFansWithLeveltwo(pageSec, 30, new ApiModel.CommonCallback<MyTeamFansBean>() {
            @Override
            public void onSuccess(MyTeamFansBean myTeamFansBean) {
                auctionMyTeamNumSec.setText("(" + myTeamFansBean.getTotal() + ")");
                auctionMyTeamAdapterSec.setNewData(myTeamFansBean.getList());
                auctionMyTeamAdapterSec.isUseEmpty(!(auctionMyTeamAdapterSec.getData().size() > 0));
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.auction_withdrawal).setOnClickListener(view -> share());
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader).setOnClickListener(view -> {
                    Intent it = new Intent(mActivity, BecomeTeamAc.class);
                    startActivity(it);
                });
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setOnClickListener(view -> {
                    Intent it = new Intent(mActivity, PreTeamLeaderIntroActivity.class);
                    startActivity(it);
                });
            }

            @Override
            public void onError(String errMsg) {

            }
        });
    }

    int totalFans = 0;

    @Override
    public void onLoadAuctionListSuccess(MyTeamFansBean myTeamFansBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        totalFans = myTeamFansBean.getTotal();
        auctionMyTeamNum.setText("(" + myTeamFansBean.getTotal() + ")");
        if (page == 1) {
            auctionMyTeamAdapter.setNewData(myTeamFansBean.getList());
        } else {
            if (myTeamFansBean.getList() != null && myTeamFansBean.getList().size() != 0) {
                auctionMyTeamAdapter.addData(myTeamFansBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        auctionMyTeamAdapter.isUseEmpty(!(auctionMyTeamAdapter.getData().size() > 0));
        auctionMyTeamAdapter.getEmptyView().findViewById(R.id.auction_withdrawal).setOnClickListener(view -> share());
        auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader).setOnClickListener(view -> {
            Intent it = new Intent(mActivity, BecomeTeamAc.class);
            startActivity(it);
        });
        auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setOnClickListener(view -> {
            Intent it = new Intent(mActivity, PreTeamLeaderIntroActivity.class);
            startActivity(it);
        });
    }

    @Override
    public void onLoadAuctionListFailed() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadHonorValueBeanSuccess(HonorValueBean honorValueBean) {
        isShow = honorValueBean.getPm_level();
//        if (honorValueBean.getPm_level() != 0) {
//        layoutTeam.setVisibility(View.VISIBLE);
//        auctionIsShowTeam.setVisibility(View.GONE);
//        auctionIsLint.setVisibility(View.GONE);
        switch (honorValueBean.getPm_level()) {
            case 0:
                auctionIdentity.setText("普通用户");
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.VISIBLE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.VISIBLE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.VISIBLE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.VISIBLE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.VISIBLE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.VISIBLE);
                break;
            case 1:
                auctionIdentity.setText("团长");
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.GONE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.GONE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.GONE);
                break;
            case 2:
                auctionIdentity.setText("区县代理");
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.GONE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.GONE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.GONE);
                break;
            case 3:
                auctionIdentity.setText("预备团长");
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader).setVisibility(View.GONE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.GONE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_pre).setVisibility(View.GONE);
                auctionMyTeamAdapter.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.VISIBLE);
                auctionMyTeamAdapterSec.getEmptyView().findViewById(R.id.empty_btn_team_leader_txt).setVisibility(View.VISIBLE);
                break;
            default:
        }
        auctionContribution.setText(honorValueBean.getMonthContributeVal() + "");//当月贡献值
        auctionContributionLastMouth.setText(honorValueBean.getLastMonthContributeVal() + "");//上个月贡献值
//        }
//        else {
//            layoutTeam.setVisibility(View.GONE);
//            auctionIsShowTeam.setVisibility(View.VISIBLE);
//            auctionIsLint.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void onLoadHonorValueBeanFailed() {

    }

    private void share() {
        shareSheetDialog = new ShareSheetDialog();
        shareSheetDialog.show(getSupportFragmentManager(), "shareSheetDialog");
    }

    @OnClick({R.id.auction_identity, R.id.auction_contribution, R.id.auction_withdrawal, R.id.auction_my_team_num, R.id.auction_regimental_commander, R.id.my_team_rule, R.id.my_contribution_rule, R.id.auction_layout_fans, R.id.auction_layout_fans_sec})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.auction_identity:
                break;
            case R.id.auction_contribution:
                break;
            case R.id.auction_withdrawal:
                shareSheetDialog = new ShareSheetDialog();
                shareSheetDialog.show(getSupportFragmentManager(), "shareSheetDialog");
                break;
            case R.id.auction_my_team_num:
                break;
            case R.id.auction_regimental_commander:
                //申请成为团长
                Intent it = new Intent(mActivity, BecomeTeamAc.class);
                startActivity(it);
                break;
            case R.id.my_team_rule:
                //团长说明
                if ("团长".equals(auctionIdentity.getText().toString())) {
                    //团长说明
                    it = new Intent(mActivity, BecomeTeamExplainAc.class);
                    startActivity(it);
                } else if ("区县代理".equals(auctionIdentity.getText().toString())) {
                    //区县代理说明
                    it = new Intent(mActivity, CountyLeaderIntroActivity.class);
                    startActivity(it);
                } else if ("预备团长".equals(auctionIdentity.getText().toString())) {
                    //预备团长说明
                    it = new Intent(mActivity, PreLeaderIntroActivity.class);
                    startActivity(it);
                } else {
                    it = new Intent(mActivity, NormalIntroActivity.class);
                    startActivity(it);
                }
                break;
            case R.id.my_contribution_rule:
                //贡献值说明
                it = new Intent(mActivity, BecomeTeamContributionAc.class);
                startActivity(it);
                break;
            case R.id.auction_layout_fans:
                type = 1;
                auctionMyTeamRecyclerView.setVisibility(View.VISIBLE);
                auctionMyTeamRecyclerViewSec.setVisibility(View.GONE);
                auctionMyTeamNumTxt.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionMyTeamNum.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionMyTeamNumSecTxt.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionMyTeamNumSec.setTextColor(getResources().getColor(R.color.color_font_black));
                ivLineTabFans.setVisibility(View.VISIBLE);
                ivLineTabFansSec.setVisibility(View.INVISIBLE);
                break;
            case R.id.auction_layout_fans_sec:
                type = 2;
                auctionMyTeamRecyclerView.setVisibility(View.GONE);
                auctionMyTeamRecyclerViewSec.setVisibility(View.VISIBLE);
                auctionMyTeamNumTxt.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionMyTeamNum.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionMyTeamNumSecTxt.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionMyTeamNumSec.setTextColor(getResources().getColor(R.color.holo_red_light));
                ivLineTabFans.setVisibility(View.INVISIBLE);
                ivLineTabFansSec.setVisibility(View.VISIBLE);
                break;
        }
    }

}
